import { http } from "@/utils/http";

export type WaAccountResult = {
  success: boolean;
  data: {
    current_page: number;
    data: Array<any>;
    first_page_url?: string;
    from?: number;
    last_page: number;
    last_page_url?: string;
    links?: Array<any>;
    next_page_url?: string;
    path?: string;
    per_page: number;
    prev_page_url?: string;
    to?: number;
    total: number;
    search_cond_arr?: Array<any>;
    status_options?: Array<any>;
    user_list?: Array<any>;
  };
};

export type WaRechargeResult = {
  success: boolean;
  data: {
    current_page: number;
    data: Array<any>;
    first_page_url?: string;
    from?: number;
    last_page: number;
    last_page_url?: string;
    links?: Array<any>;
    next_page_url?: string;
    path?: string;
    per_page: number;
    prev_page_url?: string;
    to?: number;
    total: number;
    search_cond_arr?: Array<any>;
    state_options?: Array<any>;
    wa_account_options?: Array<any>;
    account_list?: Array<any>;
    user_list?: Array<any>;
  };
};

export type WaAccountItem = {
  id: number;
  wa_account: string;
  admin_user_id?: number;
  remark?: string;
  is_drainage?: boolean;
  creator_id?: number;
  created_at: string;
  updated_at: string;
  admin_user_name?: string;
  creator_name?: string;
  status_text?: string;
  is_drainage_text?: string;
};

export type WaRechargeItem = {
  id: number;
  account_id: number;
  amount: number;
  remark?: string;
  state: number;
  feedback_at?: string;
  feedbacker_id?: number;
  feedback_content?: string;
  creator_id?: number;
  created_at: string;
  updated_at: string;
  wa_account?: string;
  creator_name?: string;
  feedbacker_name?: string;
  state_text?: string;
  feedback_at_text?: string;
  created_at_text?: string;
};

export type WaAccountForm = {
  wa_account: string;
  admin_user_id?: number;
  remark?: string;
  is_drainage?: boolean;
  status?: number;
};

export type WaRechargeForm = {
  account_id: number;
  amount: number;
  remark?: string;
};

export type WaRechargeProcessForm = {
  state: number;
  feedback_content?: string;
};

export type WaRechargeBatchProcessForm = {
  ids: number[];
  state: number;
  feedback_content?: string;
};

export type WaRechargeBatchCreateForm = {
  user_id: number;
  applications: Array<{
    account_id: number;
    amount: number;
  }>;
  remark?: string;
};

export type SearchParams = {
  page?: number;
  size?: number;
  wa_account?: string;
  admin_user_id?: number;
  remark?: string;
  status?: number;
  is_drainage?: number;
  account_id?: number;
  state?: number;
  creator_id?: number;
  amount?: number;
  time?: string[];
};

// WhatsApp账号管理API
export const waAccountApi = {
  // 获取账号列表
  list: (params?: SearchParams) => {
    return http.request<WaAccountResult>("get", "/wa-accounts", { params });
  },

  // 创建账号
  create: (data: WaAccountForm) => {
    return http.request<any>("post", "/wa-accounts", { data });
  },

  // 获取账号详情
  detail: (id: number) => {
    return http.request<any>("get", `/wa-accounts/${id}`);
  },

  // 更新账号
  update: (id: number, data: WaAccountForm) => {
    return http.request<any>("put", `/wa-accounts/${id}`, { data });
  },

  // 删除账号
  delete: (id: number) => {
    return http.request<any>("delete", `/wa-accounts/${id}`);
  },

  // 启用/禁用账号
  enable: (id: number) => {
    return http.request<any>("put", `/wa-accounts/enable/${id}`);
  },

  // 获取可用账号列表（用于下拉选择）
  available: () => {
    return http.request<any>("get", "/wa-accounts-available");
  }
};

// WhatsApp充值申请管理API
export const waRechargeApi = {
  // 获取申请列表
  list: (params?: SearchParams) => {
    return http.request<WaRechargeResult>("get", "/wa-recharge", { params });
  },

  // 创建申请
  create: (data: WaRechargeForm) => {
    return http.request<any>("post", "/wa-recharge", { data });
  },

  // 获取申请详情
  detail: (id: number) => {
    return http.request<any>("get", `/wa-recharge/${id}`);
  },

  // 更新申请
  update: (id: number, data: WaRechargeForm) => {
    return http.request<any>("put", `/wa-recharge/${id}`, { data });
  },

  // 删除申请
  delete: (id: number) => {
    return http.request<any>("delete", `/wa-recharge/${id}`);
  },

  // 处理申请（审核）
  process: (id: number, data: WaRechargeProcessForm) => {
    return http.request<any>("post", `/wa-recharge/process/${id}`, { data });
  },

  // 批量处理申请
  batchProcess: (data: WaRechargeBatchProcessForm) => {
    return http.request<any>("post", "/wa-recharge/batch-process", { data });
  },

  // 批量创建申请
  batchCreate: (data: WaRechargeBatchCreateForm) => {
    return http.request<any>("post", "/wa-recharge/batch-create", { data });
  },

  // 获取统计数据
  statistics: () => {
    return http.request<any>("get", "/wa-recharge-statistics");
  }
};

// 状态常量
export const WA_RECHARGE_STATES = {
  PENDING: 1,    // 待处理
  APPROVED: 2,   // 已充值
  REJECTED: 3    // 已驳回
};

// 状态颜色映射
export const WA_RECHARGE_STATE_COLORS = {
  [WA_RECHARGE_STATES.PENDING]: "warning",
  [WA_RECHARGE_STATES.APPROVED]: "success",
  [WA_RECHARGE_STATES.REJECTED]: "danger"
};

// 状态文本映射
export const WA_RECHARGE_STATE_TEXTS = {
  [WA_RECHARGE_STATES.PENDING]: "待处理",
  [WA_RECHARGE_STATES.APPROVED]: "已充值",
  [WA_RECHARGE_STATES.REJECTED]: "已驳回"
}; 